﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Resources;
using System.Windows.Markup;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using DragDraw.Util;

namespace DragDraw.Tool
{
    public partial class LiveTopBG : UserControl
    {
        private WebClient m_xapLoader = new WebClient();
        private string m_url;
        public LiveTopBG()
        {
            InitializeComponent();
            m_xapLoader.OpenReadCompleted += m_xapLoader_OpenReadCompleted;
        }

        void m_xapLoader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if ((e.Error == null) && (e.Cancelled == false))
            {
                Assembly assembly;
                string[] strs = m_url.Split(new char[1] { '/' });
                string name = strs[strs.Length - 1];

                string source = name.Substring(0, name.Length - 3) + "dll";
                string objName = name.Substring(0, name.Length - 3) + "Page";

                ErrorManager.Instance.ShowDebugMessage(source);
                ErrorManager.Instance.ShowDebugMessage(objName);

                try
                {
                    StreamResourceInfo streamInfo = Application.GetResourceStream(
                                 new StreamResourceInfo(e.Result,
                                 "application/binary"),
                                 new Uri(source, UriKind.Relative));
                    AssemblyPart assemblyPart = new AssemblyPart();
                    assembly = assemblyPart.Load(streamInfo.Stream);

                    UserControl uc = (UserControl)assembly.CreateInstance(objName);
                    if (uc == null)
                        ErrorManager.Instance.ShowDebugMessage("uc == null");
                    XapRoot.Children.Clear();
                    XapRoot.Children.Add(uc);
                }
                catch
                {
                    ErrorManager.Instance.ShowWarnMessage("加载Silverlight背景失败");
                }
            }
        }

        public void SetMedia(string url)
        {
            XapRoot.Children.Clear();
            try
            {
                m_url = url;
                MediaRoot.Source = new Uri(url);
            }
            catch
            {
                m_url = null;
                MediaRoot.Source = null;
            }
        }

        public void SetXap(string url)
        {
            m_url = url;
            MediaRoot.Source = null;
            m_xapLoader.OpenReadAsync(new Uri(url));
        }

        private void MediaRoot_MediaEnded(object sender, RoutedEventArgs e)
        {
            if(m_url != null)
                MediaRoot.Play();
        }
    }
}
